<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/7
 * Time: 3:30 PM
 */

include_once "../../Util/DebugUtil/DebugUtil.php";

$lamp = array(
    "l" => "Linux",
    "a" => "Apache",
    "m" => "Mysql",
    "p" => "PHP",
);


// sort 对value进行排序 rsort逆序排序
DebugUtil::printDebugSeperator();
$arrToSort = $lamp;
sort($arrToSort);
print_r($arrToSort);
// Output:
//Array
//(
//    [0] => Apache
//    [1] => Linux
//    [2] => Mysql
//    [3] => PHP
//)


// asort 对value进行排序，保留索引关系  arsort 逆序排序
DebugUtil::printDebugSeperator();
$arrToSort = $lamp;
asort($arrToSort);
print_r($arrToSort);
// Output:
//Array
//(
//    [a] => Apache
//    [l] => Linux
//    [m] => Mysql
//    [p] => PHP
//)


// ksort 对key进行排序 krsort 逆序排序
DebugUtil::printDebugSeperator();
$arrToSort = $lamp;
ksort($arrToSort);
print_r($arrToSort);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => Apache
//    [m] => Mysql
//    [p] => PHP
//)


DebugUtil::printDebugSeperator();

$data = array(
    "file.txt",
    "file1.txt",
    "file11.txt",
    "file2.txt",
    "file22.txt",
    "FILE.TXT",
    "FILE1.TXT",
);

// natsort
$arrToSort = $data;
DebugUtil::printDebugSeperator();
natsort($arrToSort);
print_r($arrToSort);
// Output:
//Array
//(
//    [5] => FILE.TXT
//    [6] => FILE1.TXT
//    [0] => file.txt
//    [1] => file1.txt
//    [3] => file2.txt
//    [2] => file11.txt
//    [4] => file22.txt
//)

DebugUtil::printDebugSeperator();
natcasesort($arrToSort);
print_r($arrToSort);
// Output:
//Array
//(
//    [5] => FILE.TXT
//    [0] => file.txt
//    [6] => FILE1.TXT
//    [1] => file1.txt
//    [3] => file2.txt
//    [2] => file11.txt
//    [4] => file22.txt
//)


DebugUtil::printDebugSeperator();
// 自定义排序
function sortByLen($one, $two)
{
    if (strlen($one) > strlen($two)) {
        return 1;
    } else if (strlen($one) == strlen($two)) {
        return 0;
    } else {
        return -1;
    }
}

// usort 用于自定义的排序规则，使用长度排序
DebugUtil::printDebugSeperator();
$arrToSort = array_values($lamp);
usort($arrToSort, sortByLen);
print_r($arrToSort);
// Output:
//Array
//(
//    [0] => PHP
//    [1] => Linux
//    [2] => Mysql
//    [3] => Apache
//)


DebugUtil::printDebugSeperator();
$arrToSort = array_values($lamp);
uasort($arrToSort, sortByLen);
print_r($arrToSort);
// Output:
//Array
//(
//    [0] => PHP
//    [1] => Linux
//    [2] => Mysql
//    [3] => Apache
//)


DebugUtil::printDebugSeperator();
$data = array(
    array("id" => 1, "soft" => "Linux", "rating" => 3),
    array("id" => 2, "soft" => "Apache", "rating" => 6),
    array("id" => 3, "soft" => "Mysql", "rating" => 2),
    array("id" => 4, "soft" => "PHP", "rating" => 2),
);

foreach ($data as $index => $value) {
    $soft[$index] = $value["soft"];
    $rating[$index] = $value["rating"];
}

DebugUtil::printDebugSeperator();
// 先从rating维度排序，再从soft维度排序
$result = array_multisort($rating, $soft, $data);
echo "result = $result\r\n";
print_r($data);

// array_multisort对对象类型的数组进行排序，指定拍戏使用的对象字段以及排序的方式[DESC/ASC]
DebugUtil::printDebugSeperator();
class Person
{
    var $name;
    var $age;
}

$persons = array();

{
    $p1 = new Person();
    $p1->name = "xiaomin";
    $p1->age = "25";
    array_push($persons, $p1);
}

{
    $p1 = new Person();
    $p1->name = "xiaozhang";
    $p1->age = "20";
    array_push($persons, $p1);
}

{
    $p1 = new Person();
    $p1->name = "xiaohuang";
    $p1->age = "28";
    array_push($persons, $p1);
}

array_multisort(array_column($persons, "age"), SORT_ASC, $persons);
print_r($persons);


